package com.example.retrofitdemo_618lw.model;

import android.widget.Toast;

import com.example.retrofitdemo_618lw.bean.LoginBean;
import com.example.retrofitdemo_618lw.common.Common;
import com.example.retrofitdemo_618lw.iface.LoginListener;
import com.example.retrofitdemo_618lw.iface.Loginiface;
import com.example.retrofitdemo_618lw.service.UserService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class LoginModel implements Loginiface {
    public static Object getLoginResult;
    private Retrofit retrofit;

    public LoginModel {
        retrofit = new Retrofit.Builder()
                .baseUrl(Common.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public void getLonginResult(String username, String pass, final LoginListener){
      UserService userService
            =retrofit.create(UserService.class);
      Call<LoginBean> call
             =userService.login(username,pass);
      call.enqueue(new Callback<LoginBean>() {
          @Override
             public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
             if (response.isSuccessful()&&response.body()!=null&&response.body().getId()!=null){

                 LoginListener.onResponse(response.body());

            }
            else LoginListener.onFail("login fail");

        }

        @Override
        public void onFailure(Call<LoginBean> call, Throwable t) {
            LoginListener.onFail(t.getMessage());
        }
    });
}
